package com.leantech.android.browser.views

import android.content.Context
import android.content.res.Resources
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatCheckBox
import androidx.collection.SimpleArrayMap
import com.leantech.android.browser.R
import com.leantech.android.browser.skin.SkinManager
import com.qmuiteam.qmui.skin.IQMUISkinHandlerView
import com.qmuiteam.qmui.skin.QMUISkinManager
import com.qmuiteam.qmui.skin.defaultAttr.IQMUISkinDefaultAttrProvider
import com.qmuiteam.qmui.util.QMUIResHelper

class SkinCheckBox @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatCheckBox(context, attrs, defStyleAttr), IQMUISkinDefaultAttrProvider,
    IQMUISkinHandlerView {

    companion object {
        private val DefaultAttrs = SimpleArrayMap<String, Int>(1)

        const val KeyButton = "button"


        init {
            DefaultAttrs.put(KeyButton, R.attr.app_skin_icon_check_box_blue)
        }
    }


    override fun handle(
        manager: QMUISkinManager,
        skinIndex: Int,
        theme: Resources.Theme,
        attrs: SimpleArrayMap<String, Int>?
    ) {
        if (attrs == null || attrs.isEmpty()) return
        for (i in 0 until attrs.size()) {
            val key = attrs.keyAt(i)
            val attr = attrs.valueAt(i)

            when (key) {
                KeyButton -> {
                    buttonDrawable = QMUIResHelper.getAttrDrawable(context, SkinManager.currentTheme(context), attr)
                }

                else -> {
                    manager.defaultHandleSkinAttr(this, theme, key, attr)
                }
            }

        }

    }


    override fun getDefaultSkinAttrs(): SimpleArrayMap<String, Int>? {
        return DefaultAttrs
    }
}